﻿@using AC.Service.DTO.LiCai
@model wychuan2.com.Areas.admin.Models.LiCai.BillModel
@{
    ViewBag.Title = "理财-明细";
    Layout = "../Shared/_Layout.cshtml";
}

@{
    var currentAccount = Model.Accounts.First(a => a.Id == Model.CurrentAccountId);
    List<ItemDTO> lstProjects = Model.Items.Where(m => m.Type == ItemType.Project).ToList();
    List<ItemDTO> lstMembers = Model.Items.Where(m => m.Type == ItemType.Member).ToList();
    string strCategories = AC.Util.JsonHelper.ToJson(Model.Categories);
    List<ItemDTO> lstBusiness = Model.Items.Where(m => m.Type == ItemType.Business).ToList();
    List<ItemDTO> lstPaymentPersons = Model.Items.Where(m => m.Type == ItemType.PaymentPerson).ToList();

    var lstExpends = Model.Categories.Where(c => c.InOutType == CategoryType.Expend && c.ParentId == 0).OrderByDescending(c => c.IsCommonUse);
    var lstExpendsChilds = Model.Categories.Where(c => c.ParentId == lstExpends.FirstOrDefault().Id).OrderByDescending(c => c.IsCommonUse);

    var lstIncomes = Model.Categories.Where(c => c.InOutType == CategoryType.Income && c.ParentId == 0).OrderByDescending(c => c.IsCommonUse);
    var lstIncomesChilds = Model.Categories.Where(c => c.ParentId == lstIncomes.FirstOrDefault().Id).OrderByDescending(c => c.IsCommonUse);
}

@section styles{
    <link href="~/Content/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="~/Content/plugins/datapicker/datepicker3.css" rel="stylesheet" />
    <link href="~/Content/plugins/mobiscroll/mobiscroll.custom-2.5.2.min.css" rel="stylesheet" />

    <style type="text/css">
        .ibox-bill { margin-bottom: 10px; }

        .ibox-title-bill { border-top: none !important; }

        .bill-item-day { }

        .bill-item-list { }

        .bill-item {
            display: block;
            padding: 0px;
            margin-bottom: 2px;
            background: #fff;
            border-bottom: 1px solid #f1f1f1;
        }

        .bill-item a {
            font-size: 14px;
            font-weight: 600;
            /*color: #1ab394;*/
            display: block;
        }

        .bill-item-price {
            font-size: 17px;
            line-height: 18px;
            font-weight: 200;
        }
    </style>
}
<div class="row">
    <div class="col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <span class="label label-info pull-right">余额:¥@currentAccount.Balance</span>
                <h5>@currentAccount.Name</h5>
            </div>
            <div class="ibox-content">
                <div class="row">
                    @{
                        var sumOfZhechu = Model.Details.Where(b => b.DetailType == BillDetailType.Expend.GetHashCode()).Sum(b => b.Price);
                        var sumOfShouru = Model.Details.Where(b => b.DetailType == BillDetailType.Income.GetHashCode()).Sum(b => b.Price);
                    }
                    @if (currentAccount.Type != 3)
                    {
                        <div class="col-md-6 col-sm-6 col-xs-6">
                            <h1 class="no-margins">¥ @sumOfShouru.ToString("0.00")</h1>
                            <div class="font-bold text-navy">
                                <small>收入</small>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-6 col-xs-6">
                            <h1 class="no-margins">¥ @sumOfZhechu.ToString("0.00")</h1>
                            <div class="font-bold text-navy">
                                <small>支出</small>
                            </div>
                        </div>
                    }
                    else
                    {
                        <div class="col-md-6 col-sm-6 col-xs-6">
                            <h1 class="no-margins">¥ @((-currentAccount.Balance).ToString("0.00"))</h1>
                            <div class="font-bold text-navy">
                                <small>当前欠款</small>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-6 col-xs-6">
                            <h1 class="no-margins">¥ @((currentAccount.AccountLimit + currentAccount.Balance).ToString("0.00"))</h1>
                            <div class="font-bold text-navy">
                                <small>可用额度</small>
                            </div>
                        </div>
                    }
                </div>
                <div class="alert alert-info">
                    <a class="btn btn-white J_s">收起</a>
                    <a class="btn btn-white J_z" disabled="disabled">展开</a>
                    <a class="btn btn-white" href="/admin/licai/fastbill">去记账</a>
                </div>
                <hr />
                @if (Model.Details.Count > 0)
                {
                    <div class="row">
                        <div class="col-lg-12">
                            @{
                                IEnumerable<IGrouping<DateTime, BillDTO>> lstDetails = Model.Details.GroupBy(b => b.ConsumeTime.Date);
                                foreach (var item in lstDetails)
                                {
                                    DateTime currentDay = item.Key;
                                    List<BillDTO> lstBills = item.ToList();

                                    decimal sumOfExpendsPrice = lstBills.Where(b => b.DetailType == BillDetailType.Expend.GetHashCode()).Sum(b => b.Price);
                                    decimal sumOfIncomePrice = lstBills.Where(b => b.DetailType == BillDetailType.Income.GetHashCode()).Sum(b => b.Price);

                                    <div class="ibox float-e-margins" style="margin-bottom: 5px;">
                                        <div class="ibox-title ibox-title-bill" style="padding-top: 0px; padding-bottom: 0px; min-height: 20px; height: 20px;">
                                            <h5>@currentDay.ToString("yyyy-MM-dd")</h5>
                                            <div class="ibox-tools">
                                                <span class="label label-primary">支：¥@sumOfExpendsPrice.ToString("0.00")</span>
                                                <span class="label label-danger">收：¥@sumOfIncomePrice.ToString("0.00")</span>

                                                <a class="collapse-link ui-sortable J_BillItem">
                                                    <i class="fa fa-chevron-up"></i>
                                                </a>
                                            </div>
                                        </div>
                                        <div class="ibox-content" style="display: block; padding-top: 5px; padding-bottom: 5px;">
                                            <div class="bill-item-list">
                                                @foreach (var bill in lstBills)
                                                {
                                                    string priceColor = "";
                                                    string textColor = "text-navy";
                                                    string text = bill.SecondCategory;
                                                    string checkedStr = "";
                                                    if (bill.DetailType == BillDetailType.Expend.GetHashCode())
                                                    {
                                                        priceColor = "text-danger";
                                                        if (bill.BaoXiao == 1)
                                                        {
                                                            textColor = "text-danger";
                                                            checkedStr = "checked='checked'";
                                                        }
                                                    }
                                                    else if (bill.DetailType == BillDetailType.Income.GetHashCode())
                                                    {
                                                        priceColor = "text-navy";
                                                    }
                                                    else if (bill.DetailType == BillDetailType.Transfer.GetHashCode())
                                                    {
                                                        var accountFrom = Model.Accounts.First(a => a.Id == bill.AccountId);
                                                        var accountTo = Model.Accounts.First(a => a.Id == bill.ToAccountId);
                                                        text = string.Format("{0} 转 {1}", accountFrom.Name, accountTo.Name);
                                                        textColor = "text-warning";
                                                    }
                                                    else if (bill.DetailType == BillDetailType.Creditor.GetHashCode())
                                                    {
                                                        if (bill.CreditorType == CreditType.JieRu.GetHashCode())
                                                        {
                                                            text = string.Format("从 {0} 借入", bill.Business);
                                                        }
                                                        else if (bill.CreditorType == CreditType.JieChu.GetHashCode())
                                                        {
                                                            text = string.Format("借出给 {0}", bill.Business);
                                                        }
                                                        else if (bill.CreditorType == CreditType.ShouKuan.GetHashCode())
                                                        {
                                                            text = string.Format("从 {0} 收债", bill.Business);
                                                        }
                                                        else
                                                        {
                                                            text = string.Format("还款给 {0}", bill.Business);
                                                        }
                                                        textColor = "text-warning";
                                                    }


                                                    <div class="bill-item" id="@("item" + bill.ID)">
                                                        <div class="row">
                                                            <div class="col-md-12">
                                                                <small class="pull-right bill-item-price @priceColor">@bill.Price</small>
                                                                <a data-toggle="collapse" class="@textColor" href="#@bill.ID">@text</a>
                                                                <small><i class="fa fa-clock-o"></i>@bill.ConsumeTime.ToString("HH:mm")  <strong>@bill.Remark</strong> </small>
                                                            </div>
                                                        </div>
                                                        <div class="row">
                                                            <div class="col-md-12" style="padding-left: 0px; padding-right: 0px;">
                                                                <div id="@bill.ID" class="panel-collapse faq-answer collapse" aria-expanded="false">
                                                                    @if (bill.DetailType == BillDetailType.Income.GetHashCode() || bill.DetailType == BillDetailType.Expend.GetHashCode())
                                                                    {
                                                                        var businessLabel = bill.DetailType == BillDetailType.Expend.GetHashCode() ? "商家" : "付款方";
                                                                        var lstFirst = lstExpends;
                                                                        var businessLst = lstBusiness;
                                                                        if (bill.DetailType == BillDetailType.Income.GetHashCode())
                                                                        {
                                                                            lstFirst = lstIncomes;
                                                                            businessLst = lstPaymentPersons;
                                                                        }
                                                                        <form class="form-horizontal" name="billEditForm">
                                                                            <input type="hidden" name="Id" value="@bill.ID" />
                                                                            <div class="row">
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-md-1 sr-only control-label">金额</label>*@
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <div class="input-group">
                                                                                                <span class="input-group-addon">￥</span>
                                                                                                <input name="txtPrice" type="text" value="@bill.Price" class="form-control" />
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-sm-1 control-label">时间</label>*@
                                                                                        <div class="col-sm-12">
                                                                                            <input name="txtConsumeTime" type="text" class="form-control datetimepicker" value="@bill.ConsumeTime.ToString("yyyy-MM-dd HH:mm")" />
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <div class="row">
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-sm-1 control-label">备注</label>*@
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <input type="text" name="txtRemark" class="form-control" placeholder="备注信息" value="@bill.Remark" />
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="sr-only control-label">类别</label>*@

                                                                                        <div class="col-md-12">
                                                                                            <div class="input-group">
                                                                                                <select class="input-sm form-control" name="selFirstCategory">
                                                                                                    @foreach (var first in lstFirst)
                                                                                                    {
                                                                                                        <option @(first.Id == bill.FirstCategoryId ? "selected" : "") value="@first.Id">@first.Name</option>
                                                                                                    }
                                                                                                </select>
                                                                                                <span class="input-group-addon">-</span>
                                                                                                <select class="input-sm form-control" name="selCategory">
                                                                                                    @{
                                                                                                        var lstSeconds = Model.Categories.Where(c => c.ParentId == bill.FirstCategoryId).OrderByDescending(c => c.IsCommonUse);
                                                                                                    }
                                                                                                    @foreach (var second in lstSeconds)
                                                                                                    {
                                                                                                        <option @(second.Id == bill.SecondCategoryId ? "selected" : "") value="@second.Id">@second.Name</option>
                                                                                                    }
                                                                                                </select>
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <div class="row">
                                                                                <div class="col-md-12">
                                                                                    <div class="input-group col-md-offset-1 m-b-xxs">
                                                                                        <select class="input-sm" name="selAccount">
                                                                                            @foreach (var account in Model.Accounts.Where(a=>a.IsEnable))
                                                                                            {
                                                                                                <option @(account.Id == bill.AccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                                            }
                                                                                        </select>
                                                                                        <select class="input-sm" name="selBusiness">
                                                                                            <option value="0">@businessLabel</option>
                                                                                            @foreach (var business in businessLst)
                                                                                            {
                                                                                                <option @(business.Id == bill.BusinessId ? "selected" : "") value="@business.Id">@business.Name</option>
                                                                                            }
                                                                                        </select>
                                                                                        <select class="input-sm" name="selMember">
                                                                                            @foreach (var member in lstMembers)
                                                                                            {
                                                                                                <option @(member.Id == bill.MemberId ? "selected" : "") value="@member.Id">@member.Name</option>
                                                                                            }
                                                                                        </select>
                                                                                        <select class="input-sm" name="selProject">
                                                                                            @foreach (var project in lstProjects)
                                                                                            {
                                                                                                <option @(project.Id == bill.ProjectId ? "selected" : "") value="@project.Id">@project.Name</option>
                                                                                            }
                                                                                        </select>
                                                                                        @if (bill.DetailType == BillDetailType.Expend.GetHashCode())
                                                                                        {
                                                                                            <label> <input type="checkbox" name="chkBaoXiao" class="i-checks" @checkedStr /> 可报销 </label>
                                                                                        }
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <hr class="m-sm" />
                                                                            <div class="row">
                                                                                <div class="col-md-12">
                                                                                    <div class="form-group m-b-sm">
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <button name="btnSave" class="btn btn-primary" type="button"><strong>保存</strong></button>
                                                                                            <button name="btnDelete" class="btn btn-primary" type="button"><strong>删除</strong></button>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </form>
                                                                    }
                                                                    else if (bill.DetailType == BillDetailType.Transfer.GetHashCode())
                                                                    {
                                                                        <form class="form-horizontal" name="billEditForm">
                                                                            <input type="hidden" name="Id" value="@bill.ID" />
                                                                            <div class="row">
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-md-1 sr-only control-label">金额</label>*@
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <div class="input-group">
                                                                                                <span class="input-group-addon">￥</span>
                                                                                                <input name="txtPrice" type="text" value="@bill.Price" class="form-control" />
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-sm-1 control-label">时间</label>*@
                                                                                        <div class="col-sm-12">
                                                                                            <input name="txtConsumeTime" type="text" class="form-control datetimepicker" value="@bill.ConsumeTime.ToString("yyyy-MM-dd HH:mm")" />
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <div class="row">
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-sm-1 control-label">备注</label>*@
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <input type="text" name="txtRemark" class="form-control" placeholder="备注信息" value="@bill.Remark" />
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="sr-only control-label">账户</label>*@

                                                                                        <div class="col-md-12">
                                                                                            <div class="input-group">
                                                                                                <select class="input-sm form-control" name="selAccount">
                                                                                                    @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                                                    {
                                                                                                        <option @(account.Id == bill.AccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                                                    }
                                                                                                </select>
                                                                                                <span class="input-group-addon">-</span>
                                                                                                <select class="input-sm form-control" name="selAccountTo">
                                                                                                    @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                                                    {
                                                                                                        <option @(account.Id == bill.ToAccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                                                    }
                                                                                                </select>
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <hr class="m-sm" />
                                                                            <div class="row">
                                                                                <div class="col-md-12">
                                                                                    <div class="form-group m-b-sm">
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <button name="btnSave" class="btn btn-primary" type="button"><strong>保存</strong></button>
                                                                                            <button name="btnDelete" class="btn btn-primary" type="button"><strong>删除</strong></button>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </form>
                                                                    }
                                                                    else if (bill.DetailType == BillDetailType.Creditor.GetHashCode())
                                                                    {
                                                                        <form class="form-horizontal" name="billEditForm">
                                                                            <input type="hidden" name="Id" value="@bill.ID" />
                                                                            <div class="row">
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-md-1 sr-only control-label">金额</label>*@
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <div class="input-group">
                                                                                                <span class="input-group-addon">￥</span>
                                                                                                <input name="txtPrice" type="text" value="@bill.Price" class="form-control" />
                                                                                            </div>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-sm-1 control-label">时间</label>*@
                                                                                        <div class="col-sm-12">
                                                                                            <input name="txtConsumeTime" type="text" class="form-control datetimepicker" value="@bill.ConsumeTime.ToString("yyyy-MM-dd HH:mm")" />
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <div class="row">
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="col-sm-1 control-label">备注</label>*@
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <input type="text" name="txtRemark" class="form-control" placeholder="备注信息" value="@bill.Remark" />
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                                <div class="col-md-6">
                                                                                    <div class="form-group m-b-xxs">
                                                                                        @*<label class="sr-only control-label">类别</label>*@
                                                                                        <div class="col-md-12">
                                                                                            <select class="form-control" name="selCreditorType">
                                                                                                <option @(1 == bill.CreditorType ? "selected" : "") value="1">借入</option>
                                                                                                <option @(2 == bill.CreditorType ? "selected" : "") value="2">借出</option>
                                                                                                <option @(3 == bill.CreditorType ? "selected" : "") value="3">收款</option>
                                                                                                <option @(4 == bill.CreditorType ? "selected" : "") value="4">还款</option>
                                                                                            </select>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <div class="row">
                                                                                <div class="col-md-12">
                                                                                    <div class="input-group col-md-offset-1 m-b-xxs">
                                                                                        <select class="input-sm" name="selBusiness">
                                                                                            <option value="0">债权人</option>
                                                                                            @foreach (var creditor in Model.Items.Where(m => m.Type == ItemType.Creditor))
                                                                                            {
                                                                                                <option @(creditor.Id == bill.BusinessId ? "selected" : "") value="@creditor.Id">@creditor.Name</option>
                                                                                            }
                                                                                        </select>
                                                                                        <select class="input-sm" name="selAccount">
                                                                                            @foreach (var account in Model.Accounts.Where(a => a.IsEnable))
                                                                                            {
                                                                                                <option @(account.Id == bill.AccountId ? "selected" : "") value="@account.Id">@(account.Name + "(" + account.Currency + ")")</option>
                                                                                            }
                                                                                        </select>

                                                                                        <input name="txtRefundTime" type="text" class="input-sm datetimepicker" value="@bill.RefundTime" placeholder="还款时间" />

                                                                                        <select class="input-sm" name="selRefundNotice">
                                                                                            <option @(bill.RefundNotice.Equals("无需提醒") ? "selected" : "")>无需提醒</option>
                                                                                            <option @(bill.RefundNotice.Equals("提醒一次") ? "selected" : "")>提醒一次</option>
                                                                                            <option @(bill.RefundNotice.Equals("每月提醒") ? "selected" : "")>每月提醒</option>
                                                                                        </select>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                            <hr class="m-sm" />
                                                                            <div class="row">
                                                                                <div class="col-md-12">
                                                                                    <div class="form-group m-b-sm">
                                                                                        <div class="col-md-offset-1 col-md-11">
                                                                                            <button name="btnSave" class="btn btn-primary" type="button"><strong>保存</strong></button>
                                                                                            <button name="btnDelete" class="btn btn-primary" type="button"><strong>删除</strong></button>
                                                                                        </div>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </form>
                                                                    }
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                }
                                            </div>
                                        </div>
                                    </div>
                                }
                            }
                        </div>
                    </div>
                }
                else
                {
                    <div class="row">
                        <div class="col-lg-12">
                            <h3>此账户暂时没有账单</h3>
                        </div>
                    </div>
                    
                }
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script src="~/Scripts/plugins/iCheck/icheck.min.js"> </script>
    @*<script src="~/Scripts/plugins/datetimepicker/jquery.datetimepicker.js"></script>*@
    <script src="~/Scripts/plugins/datapicker/bootstrap-datepicker.js"> </script>
    @*<script src="~/Scripts/plugins/mobiscroll/mobiscroll.core-2.5.2.js"></script>*@
    <script type="text/javascript" src="~/Scripts/plugins/mobiscroll/mobiscroll.custom-2.5.2.min.js"> </script>
    <script type="text/javascript" src="~/Scripts/plugins/mobiscroll/mobiscroll.datetime-2.5.1.js"> </script>
    <script type="text/javascript" src="~/Scripts/plugins/mobiscroll/mobiscroll.datetime-2.5.1-zh.js"> </script>

    <script>
        window.categories = @Html.Raw(strCategories);
    </script>
    <script src="~/Scripts/licai/detail.js"> </script>
}